<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: setup_step3.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	// File being called directly, end it
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

displayHeader();

if ($dbCore->connected)
{
	// We have a connection! Roger!
	// Does a module exist? If not, we start with core
	if (empty($_POST["setupmodule"]))
	{
		$_POST["setupmodule"] = "core";
	}

	// We have the module list
	$modulelist = $_SETUP["modules"];
	if (in_array($_POST["setupmodule"], $modulelist) && is_numeric($_POST["page"]))
	{
		$modulename = $_POST["setupmodule"];

		// Require the module file
		require_once ("./modules/".cleanVar($modulename)."/setup.php");

		// Get total number of pages for this module, the _swift_install should be declared in module setup file
		eval("\$moduletotalpages = _". cleanVar(trim($modulename)) ."_install(\"getpages\");");

		// Display Current Page etc
		displayMessage("Module: $modulename (Page ". $_POST["page"] ." of $moduletotalpages)");

		// Build the query for this very page
		eval("_". cleanVar(trim($modulename)) ."_install(\"buildpage\", \$_POST[\"page\"]);");

		// Execute the queries
		execAllQueries();

		// Calculate next step page
		if ($_POST["page"] >= $moduletotalpages)
		{
			// We have crossed the page or equalled our page, we need to move onto the first index of next module
			$currentkey = array_search($modulename, $modulelist);
			for ($ii=$currentkey+1; $ii<count($modulelist); $ii++)
			{
				// Does this module exist?
				if (file_exists("./modules/".cleanVar($modulelist[$ii])) && is_dir("./modules/".cleanVar($modulelist[$ii])))
				{
					// Set this as next module and break
					$newkey = $ii;
					$_SWIFT["execmodule"] = $modulelist[$ii];
					$_SWIFT["execpage"] = "1";
					break;
				}
			}
		} else {
			// We are not more than it yet, increment it
			$_SWIFT["execmodule"] = $modulename;
			$_SWIFT["execpage"] = $_POST["page"]+1;
		}
	} else {
		trigger_error(sprintf("Setup failed, %s module does not exist", $_POST["setupmodule"]), E_USER_ERROR);
	}
} else {
	$_SWIFT["error"] = true;
}
displayFooter();
?>